//
//  PlaceViewController.m
//  HeartTravel
//
//  Created by dlios on 15-6-30.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PlaceViewController.h"
#import "UIImageView+WebCache.h"
#import "MyCell.h"
#import "HTTPTool.h"
#import "ImageViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface PlaceViewController ()
@property(nonatomic, retain)UIImageView *imageView;
@property(nonatomic, assign)BOOL isLike;

@end

@implementation PlaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    NSLog(@"%@",self.bId);
    [self createData];
    [self createView];
    
}

//解析数据
- (void)createData{
    
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.type,self.bId] body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        //        解析imageView的图片
        NSArray *hottest_placesArr = dic[@"hottest_places"];
        NSDictionary *photoDic = hottest_placesArr[0];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:photoDic[@"photo"]]];
        
        //        解析nameLabel数据
        UILabel *nameLabel = (UILabel *)[self.imageView viewWithTag:1002];
        nameLabel.text = dic[@"name_orig"];
        self.title = dic[@"name_orig"];
        //        解析wentAndLikeLabel数据
        UILabel *wentAndLikeLabel = (UILabel *)[self.imageView viewWithTag:1003];
        wentAndLikeLabel.text = [NSString stringWithFormat:@"%@ 去过 / %@ 喜欢",dic[@"visited_count"],dic[@"wish_to_go_count"]];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)createView{
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:view];
    [view release];
    view.tag = 1001;

//  创建imageView
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 250)];
    [view addSubview:self.imageView];
    [self.imageView release];
    
//    创建nameLabel接收旅游地名
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 120, 30)];
    [self.imageView addSubview:nameLabel];
    [nameLabel release];
    nameLabel.font = [UIFont systemFontOfSize:20];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.tag = 1002;
    
//   创建label接收去过人数和喜欢人数
    UILabel *wentAndLikeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 230, 180, 20)];
    [self.imageView addSubview:wentAndLikeLabel];
    [wentAndLikeLabel release];
    wentAndLikeLabel.font = [UIFont systemFontOfSize:10];
    wentAndLikeLabel.textColor = [UIColor whiteColor];
    wentAndLikeLabel.tag = 1003;
    
    
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-gezicomfanhui1"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    
//   喜欢按钮
    UIBarButtonItem *likeButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-xihuan1"] style:UIBarButtonItemStylePlain target:self action:@selector(like:)];
//   分享按钮
    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(share:)];
    NSArray *buttonArr = [NSArray arrayWithObjects:likeButton,shareButton,nil];
    self.navigationItem.rightBarButtonItems = buttonArr;
    [shareButton release];
    [likeButton release];
    self.isLike = NO;
    
    
    //    imageView设置点击方法
    self.imageView.userInteractionEnabled = YES;
//    添加点击方法
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.imageView addGestureRecognizer:tap];

    
////    不可错过
//    UIWebView *noMissWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 250, WIDTH, HEIGHT- 49 - 64)];
//    NSString *str = [NSString stringWithFormat:@"http://breadtrip.com/mobile/destination/1/%@/intro/",self.bId];
//    NSURL *url = [NSURL URLWithString:str];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    [self.view addSubview:noMissWebView];
//    [noMissWebView loadRequest:request];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    ImageViewController *imageView = [[ImageViewController alloc] init];
    [self.navigationController pushViewController:imageView animated:YES];
    imageView.bId = self.bId;
    imageView.type = self.type;
    imageView.titleName = self.title;
//    NSLog(@"%@",self.type);
    [imageView release];
}

//返回按钮方法
- (void)back:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}
//喜欢按钮方法
- (void)like:(UIButton *)button{
    if (self.isLike == NO) {
        self.isLike = YES;
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-xihuanjiaohu"]];
    }else if(self.isLike == YES){
      [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-xihuan1"]];
        self.isLike = NO;
    }
}
//分享按钮方法
- (void)share:(UIButton *)button{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
